import urlConfig from './config.js'

const request = {}
const headers = {}

// 这个power是啥
// 下边判断headers请求头的。怎么看现在加没加上
request.allRequest = (url, method, data) => {
  let token = uni.getStorageSync('token');

  var headers = {
    // 'Content-Type': 'application/json;charset=UTF-8',
    'Content-Type': 'application/x-www-form-urlencoded',
    // "Content-Type": "application/json"
  };

  return uni.request({
    url: urlConfig + url,
    method,
    data: data,
    dataType: 'json',
    header: headers
  }).then(res => {
    if (res[1].statusCode == 200 && res[1].data.code == 200) {
      return res[1].data
    } else {
      throw res[1].data
    }
  }).catch(parmas => {
    // 这里是错误的回调
    switch (parmas.code) {
      // case 200:
      //   return Promise.resolve(parmas.data)
      case 201:
        uni.showToast({
          title: parmas.msg,
          icon: 'none'
        })
        // 返回了失败Promise 并把参数 传递到接口的失败回调
        return Promise.reject(parmas)
      default:

        return Promise.reject(parmas)
        break
    }



  })
}
export default request
